package com.chijun.mform.process.view;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import com.alibaba.fastjson.JSON;
import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.chijun.mform.create.dialog.ISelectProcessPerson;
import com.chijun.mform.create.dialog.SelectPersonNewDialog;
import com.chijun.mform.create.fragment.FormAddProcessFragment;
import com.chijun.mform.full.mode.ModeFormFullApprovePerson;
import com.chijun.mform.process.IHandlerLoadFormFull;
import com.chijun.mform.process.dialog.DialogPingLun;
import com.chijun.mform.process.dialog.DialogShenPi;
import com.chijun.mform.process.IWaitUploadCompleteListener;
import com.chijun.mform.process.IWritePingJiaCallBack;
import com.chijun.mform.process.ModeServerFullData;
import com.example.ccj.b.c.custom.ImageAndTextButton;
import com.example.ccj.b.c.custom.tree.TreeMode;
import com.example.ccj.b.c.dialog.DialogButtonOnClickListener;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.b.c.dialog.TipDialog;
import com.example.ccj.u.GenerateId;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.TimeUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.server.MyObserver;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 审批模块view
 * 加载当前人员可对当前表单做出的动作
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/28 6:31
 */
public class ViewLoadApprovalControl extends LinearLayout implements View.OnClickListener {
    ModeServerFullData serverFullData;
    IWritePingJiaCallBack iWritePingJiaCallBack;
    IHandlerLoadFormFull iHandlerLoadFormFull;

    public ViewLoadApprovalControl(Context context, ModeServerFullData serverFullData, IWritePingJiaCallBack iWritePingJiaCallBack) {
        super(context);
        this.serverFullData = serverFullData;
        this.iWritePingJiaCallBack = iWritePingJiaCallBack;
        init();
    }

    public ViewLoadApprovalControl(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    ImageAndTextButton pl,zj,sp;

    /**
     *
     * @param serverFullData 填写的表单数据
     * @param iHandlerLoadFormFull 与服务器交互的回调
     * @param iWritePingJiaCallBack 评价或审批上传成功回调 主要用于在流程模块添加评论或者更新审批状态
     */
    public void setData(ModeServerFullData serverFullData, IHandlerLoadFormFull iHandlerLoadFormFull, IWritePingJiaCallBack iWritePingJiaCallBack){
        this.serverFullData = serverFullData;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        this.iWritePingJiaCallBack = iWritePingJiaCallBack;
        init();
    }


    private void init(){
        inflate(getContext(), R.layout.view_load_approval,this);
        pl = findViewById(R.id.view_load_approval_pl);
        zj = findViewById(R.id.view_load_approval_zj);
        sp = findViewById(R.id.view_load_approval_sp);
//        jj = findViewById(R.id.view_load_approval_jj);

        LinearLayout plLayout = findViewById(R.id.view_load_approval_pl_layout);
        LinearLayout zjLayout = findViewById(R.id.view_load_approval_zj_layout);
        LinearLayout spLayout = findViewById(R.id.view_load_approval_sp_layout);


        pl.setOnClickListener(this);
        zj.setOnClickListener(this);
        sp.setOnClickListener(this);
//        jj.setOnClickListener(this);


        //当前登录人员是审批人时显示审批模块 否则隐藏审批模块
        if (serverFullData.isDoingPerson) {
            zjLayout.setVisibility(VISIBLE);
            spLayout.setVisibility(VISIBLE);
//            jj.setVisibility(VISIBLE);
        } else {
            zjLayout.setVisibility(GONE);
            spLayout.setVisibility(GONE);
//            jj.setVisibility(GONE);
        }
    }

    //转交的本质就是获取当前审批人在其下添加一个审批人并把当前登录人设为转交，把转交人设为审批中
    //评论就是在当前审批人面下添加一个审批人
    @Override
    public void onClick(View v) {
        //评论
        if (v.getId() == R.id.view_load_approval_pl) {
            addPingLun();
            return;
        }
        //转交
        if (v.getId() == R.id.view_load_approval_zj) {
            zj();
            return;
        }
        //审批
        if (v.getId() == R.id.view_load_approval_sp) {
            shenPi();
            return;
        }
    }


    private void shenPi() {
        DialogShenPi shenPi = new DialogShenPi(getContext(), serverFullData, iHandlerLoadFormFull,new IWritePingJiaCallBack() {
            @Override
            public void onSuccessPl(long a, ModeFormFullApprovePerson person) {}
            @Override
            public void onSuccessSp() {
                iWritePingJiaCallBack.onSuccessSp();
            }
        });
        shenPi.show();
    }


    /**
     * 转交
     */
    private void zj(){

        //流程表单添加方法 当前状态时审批中并且当前的审批人不为空 放入当前审批人下
        if (serverFullData.launchedForm.state == 1 && serverFullData.isDoingPerson && serverFullData.thisFormFullApprovePerson != null) {
            //获取应该添加在什么位置
            //深度复制当前审批人
            String jsonString = JSON.toJSONString(serverFullData.thisFormFullApprovePerson);
            ModeFormFullApprovePerson zhuanJiao = JSON.parseObject(jsonString, ModeFormFullApprovePerson.class);
            zhuanJiao.id = GenerateId.getId();
            zhuanJiao.state = FormAddProcessFragment.SHEN_PI_STATE_ZHENG_ZAI_SHEN;

            SelectPersonNewDialog dialog = new SelectPersonNewDialog(getContext(), "选择转交人", null, 1, false, false, new ISelectProcessPerson() {
                @Override
                public void onOver(List<TreeMode> treeModeList) {
                    if (treeModeList != null && !treeModeList.isEmpty()) {
                        TipDialog tipDialog = new TipDialog(getContext(), "确定要转交给 < " + treeModeList.get(0) + " >?", new DialogButtonOnClickListener() {
                            @Override
                            public void dialogSure() {
                                LoadingDialog loadingDialog = new LoadingDialog(getContext());
                                loadingDialog.showLoading();
                                zhuanJiao.note =  serverFullData.thisFormFullApprovePerson.personName + "转交";
                                zhuanJiao.personId = treeModeList.get(0).dataId;
                                zhuanJiao.personName = treeModeList.get(0).name;


                                if (iHandlerLoadFormFull != null) {
                                    iHandlerLoadFormFull.uploadZhuanJiao(zhuanJiao, String.valueOf(serverFullData.thisFormFullApprovePerson.id), new IWaitUploadCompleteListener() {
                                        @Override
                                        public void onComplete() {
                                            iWritePingJiaCallBack.onSuccessPl(serverFullData.thisFormFullApprovePerson.id ,zhuanJiao);
                                            sp.setVisibility(GONE);
                                            zj.setVisibility(GONE);
                                            loadingDialog.dismiss();
                                        }

                                        @Override
                                        public void onError(String value) {
                                            loadingDialog.dismiss();
                                        }
                                    });
                                }
                            }
                        });
                        tipDialog.show();
                    } else {
                        ToastUtils.showShort("未选择转交人");
                        LogUtils.w("未选择转交人");
                    }
                }
            });
            dialog.show();
        } else {
            ToastUtils.showShort("转交失败，不是当前审批人无法转交！" );
            LogUtils.e("转交失败，不是当前审批人无法转交！,这是个很严重的错误，不是当前审批人不应该显示转交界面！");
        }
    }

    private void addPingLun(){
        //打开添加评论界面
        DialogPingLun pingLun = new DialogPingLun(getContext(), serverFullData, new DialogPingLun.IResultPingLunContentCallBack() {
            @Override
            public void onResult(long id, ModeFormFullApprovePerson person) {
                iWritePingJiaCallBack.onSuccessPl(id,person);
            }
            @Override
            public void onCancel() {}
        },iHandlerLoadFormFull);
        pingLun.show();
    }

    private void addOnePlPerson(long a, ModeFormFullApprovePerson person){
        try {
            LoadingDialog loadingDialog = new LoadingDialog(getContext());
            loadingDialog.showLoading();
            if (iHandlerLoadFormFull != null) {
                iHandlerLoadFormFull.uploadPingLun(person, new IWaitUploadCompleteListener() {
                    @Override
                    public void onComplete() {
                        //添加流程
                        iWritePingJiaCallBack.onSuccessPl(a,person);
                        ToastUtils.showShort("评论成功");
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onError(String value) {
                        loadingDialog.dismiss();
                    }
                });
            } else {
                LogUtils.e("FormBuilder类中的iHandlerLoadFormFull接口未实例化，无法上传评论");
            }
        } catch (Exception e) {
            LogUtils.e("上传评论失败：" + e.getMessage());
            ToastUtils.showShort("评论失败：" + e.getMessage());
        }
    }



}
